package com.hxzy.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author wzm
 * @create 2023-10-10 15:22
 * project_name:stu_sys
 */
//阿里云oss上传工具类
//将普通对象注入到ioc容器
@Slf4j
@Component
public class AliOssUtils {
    //配置一些字符串信息
   /* String endpoint = "https://oss-cn-beijing.aliyuncs.com";//区域节点
    String accessKeyId = "LTAI5tCK2sqtgPzah3ESTCdV";//id
    String accessKeySecret = "ka0boPFNNv73k6czI4OzOs2wcZFx66";//Secret
    String bucketName = "web2308-01";//bucket*/

    //通过@Value注解获取配置文件中的配置信息 @Value("${key}")
 /*   @Value("${aliyun.oss.endpoint}")
    private String endpoint;//区域节点
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId ;//id
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;//Secret
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;//bucket*/
    //此处省略200个属性

    //获取阿里云OssConfigBean的对象
    @Autowired
    private AliyunOssConfigBean aliyunOssConfigBean;


    //文件上传的方法
    public String upload(MultipartFile multipartFile) throws IOException {
        //挨个获取
//        String accessKeyId = aliyunOssConfigBean.getAccessKeyId();

        //获取文件的输入流
        InputStream inputStream = multipartFile.getInputStream();

        //文件名防止覆盖
        String originalFilename = multipartFile.getOriginalFilename();
        String newFileName= UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));


        // 使用代码嵌入访问密钥
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliyunOssConfigBean.getAccessKeyId(), aliyunOssConfigBean.getAccessKeySecret());
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(aliyunOssConfigBean.getEndpoint(), credentialsProvider);
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOssConfigBean.getBucketName(), newFileName, inputStream);
        // 创建PutObject请求。
        ossClient.putObject(putObjectRequest);

        //文件名 https://web2308-01.oss-cn-beijing.aliyuncs.com/110.txt
        //enpoint: https://oss-cn-beijing.aliyuncs.com

        //文件的访问路径： https://buket名.oss-cn-beijing.aliyuncs.com+/文件名

        //          https: +  //+ buket名 +.+ oss-cn-beijing.aliyuncs.com
        String url = aliyunOssConfigBean.getEndpoint().split("//")[0] + "//" + aliyunOssConfigBean.getBucketName() + "." + aliyunOssConfigBean.getEndpoint().split("//")[1] + "/" + newFileName;
        log.info("地址为：{}",url);
        //关闭ossClient
        if (ossClient != null) {
            ossClient.shutdown();
        }
        return url;
    }



}
